package org.jeecg.modules.demo.jxcx.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.po.JxcOpenuser;
import org.jeecg.modules.demo.jxcx.po.JxcUser;
import org.jeecg.modules.demo.jxcx.po.SystemVo;
import org.jeecg.modules.demo.jxcx.service.*;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * (JxcUser)表控制层
 *
 * @author makejava
 * @since 2024-07-23 17:29:01
 */
@RestController
@RequestMapping("jxcUser")
public class JxcUserController{
    private static Logger logger = LoggerFactory.getLogger(JxcUserController.class);

    /**
     * 服务对象
     */
    @Resource
    private JxcUserService jxcUserService;
    @Resource
    private JxcOpenuserService jxcOpenuserService;
    @Resource
    private JxcSaleService jxcSaleService;
    @Resource
    private JxcStockService jxcStockService;
    @Resource
    private JxcCostService jxcCostService;

    /**
     * 登录
     *
     * @throws IOException
     * @throws
     */
    @RequestMapping("/login")
    @ResponseBody
    public R login(@RequestBody JSONObject systemVo) {
        R result = null;
        try {
            logger.info("登录请求：" + systemVo.toString());
            String openid = systemVo.getString("openid");
            String account = systemVo.getString("account");
            String password = systemVo.getString("password");
            JxcUser byAccount = jxcUserService.getByAccount(account);
            JxcOpenuser jxcOpenuserby = jxcOpenuserService.getByaccount(account);
            if(byAccount == null){
                logger.info("账号不存在");
                return new R().error("账号不存在");
            }
            if(!byAccount.getPassword().equals(password)){
                logger.info("密码错误");
                result = new R().error("密码错误");
            }else {
                JxcOpenuser byAccount1 = jxcOpenuserService.getByAccount(account,openid);
                if (jxcOpenuserby == null){
                    JxcOpenuser jxcOpenuser = new JxcOpenuser();
                    jxcOpenuser.setAccount(account);
                    jxcOpenuser.setOpenid(openid);
                    jxcOpenuser.setIsDefult("1");
                    jxcOpenuserService.save(jxcOpenuser);
                    logger.info("添加默认登录人"+jxcOpenuser.toString());
                    result = new R().ok("登录成功");
                }else {
                    logger.info("登录人"+jxcOpenuserby.toString()+"已在另一微信登录");
//                    jxcOpenuserService.updateIsDefult2(account,openid);
                    result = new R().error("已在另一微信登录");
                }

            }
        } catch (Exception e) {
            result = new R().error(e.getMessage());
        }
        return result;
    }
    /**
     * 获取今日出货量进货量利润
     *
     * @throws IOException
     * @throws
     */
    @RequestMapping("/getamount")
    @ResponseBody
    public JSONObject getamount(@RequestBody JSONObject json) throws Exception {
        String day = json.getString("date");
        String companyId = json.getString("companyId");
        logger.info("获取今日出货量进货量利润请求：" + json.toString());
        JSONObject result = new JSONObject();
        JSONObject data = new JSONObject();
        if (jxcStockService.getamount(day,companyId) == null){
            data.put("Stockamount", "无数据");
            logger.info(day+"进货金额无数据");
        }else {
            BigDecimal bigDecimal = new BigDecimal(jxcStockService.getamount(day, companyId));
            data.put("Stockamount",bigDecimal );
            logger.info(day+"进货金额：" + bigDecimal);
        }
        if (jxcSaleService.getamount(day,companyId) == null){
            data.put("Saleamount", "无数据");
            logger.info(day+"销售金额无数据");
        }else {
            BigDecimal bigDecimal = new BigDecimal(jxcSaleService.getamount(day, companyId));
            data.put("Saleamount", bigDecimal);
            logger.info(day+"销售金额：" + bigDecimal);
        }
        String amount = "";
        BigDecimal Costamount = new BigDecimal(0);
        if (jxcCostService.getamount(day,companyId) != null) {
              amount = jxcCostService.getamount(day,companyId);

            Costamount = new BigDecimal(amount);
            logger.info(day+"成本金额：" + Costamount);
            logger.info("------------");
            data.put("Costamount", Costamount);
            if (data.getString("Stockamount").equals("无数据")){
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售无进货有成本
                    data.put("profit","-"+Costamount);
                }else {
                    //有销售无进货有成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
                }
            }else {
                BigDecimal bigDecimal = new BigDecimal(data.getString("Stockamount")).add(Costamount);
                logger.info("有进货有成本");
                logger.info("进货和成本总和"+bigDecimal);
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售有进货有成本
                    data.put("profit","-"+bigDecimal);
                }else {
                    //有销售有进货有成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(bigDecimal));
                    logger.info("有销售有进货有成本");
                }
            }
        }else {
            if (data.getString("Stockamount").equals("无数据")){
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售无进货无成本
                    data.put("profit","无数据");
                }else {
                    //有销售无进货无成本
                    data.put("profit",data.getString("Saleamount"));
                }
            }else {
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售有进货无成本
                    data.put("profit","-"+data.getString("Stockamount"));
                }else {
                    //有销售有进货无成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(new BigDecimal(data.getString("Stockamount"))));
                }
            }

            data.put("Costamount", "无数据");
            logger.info(day+"成本金额无数据");
        }
        result.put("data", data);
        logger.info("result.toString()--------------");
        logger.info(result.toString());
        return result;
    }
    /**
     * 获取本月出货量进货量利润
     *
     * @throws IOException
     * @throws
     */
    @RequestMapping("/getyueamount")
    @ResponseBody
    public JSONObject getyueamount(@RequestBody JSONObject json) throws Exception {
        String day = json.getString("date");
        String companyId = json.getString("companyId");
        logger.info("获取今日出货量进货量利润请求：" + json.toString());
        JSONObject result = new JSONObject();
        JSONObject data = new JSONObject();
        if (jxcStockService.getyueamount(day,companyId) == null){
            data.put("Stockamount", "无数据");
            logger.info(day+"进货金额无数据");
        }else {
            BigDecimal bigDecimal = new BigDecimal(jxcStockService.getyueamount(day, companyId));
            data.put("Stockamount",bigDecimal );
            logger.info(day+"进货金额：" + bigDecimal);
        }
        if (jxcSaleService.getyueamount(day,companyId) == null){
            data.put("Saleamount", "无数据");
            logger.info(day+"销售金额无数据");
        }else {
            BigDecimal bigDecimal = new BigDecimal(jxcSaleService.getyueamount(day, companyId));
            data.put("Saleamount", bigDecimal);
            logger.info(day+"销售金额：" + bigDecimal);
        }
        String amount = "";
        BigDecimal Costamount = new BigDecimal(0);
        if (jxcCostService.getyueamount(day,companyId) != null) {
            amount = jxcCostService.getyueamount(day,companyId);

            Costamount = new BigDecimal(amount);
            logger.info(day+"成本金额：" + Costamount);
            logger.info("------------");
            data.put("Costamount", Costamount);
            if (data.getString("Stockamount").equals("无数据")){
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售无进货有成本
                    data.put("profit","-"+Costamount);
                }else {
                    //有销售无进货有成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
                }
            }else {
                BigDecimal bigDecimal = new BigDecimal(data.getString("Stockamount")).add(Costamount);
                logger.info("有进货有成本");
                logger.info("进货和成本总和"+bigDecimal);
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售有进货有成本
                    data.put("profit","-"+bigDecimal);
                }else {
                    //有销售有进货有成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(bigDecimal));
                    logger.info("有销售有进货有成本");
                }
            }
        }else {
            if (data.getString("Stockamount").equals("无数据")){
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售无进货无成本
                    data.put("profit","无数据");
                }else {
                    //有销售无进货无成本
                    data.put("profit",data.getString("Saleamount"));
                }
            }else {
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售有进货无成本
                    data.put("profit","-"+data.getString("Stockamount"));
                }else {
                    //有销售有进货无成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(new BigDecimal(data.getString("Stockamount"))));
                }
            }

            data.put("Costamount", "无数据");
            logger.info(day+"成本金额无数据");
        }
        result.put("data", data);
        logger.info("result.toString()--------------");
        logger.info(result.toString());
        return result;
    }
    /**
     * 获取本年出货量进货量利润
     *
     * @throws IOException
     * @throws
     */
    @RequestMapping("/getnianamount")
    @ResponseBody
    public JSONObject getnianamount(@RequestBody JSONObject json) throws Exception {
        String day = json.getString("date");
        String companyId = json.getString("companyId");
        logger.info("获取今日出货量进货量利润请求：" + json.toString());
        JSONObject result = new JSONObject();
        JSONObject data = new JSONObject();
        if (jxcStockService.getnianamount(day,companyId) == null){
            data.put("Stockamount", "无数据");
            logger.info(day+"进货金额无数据");
        }else {
            BigDecimal bigDecimal = new BigDecimal(jxcStockService.getnianamount(day, companyId));
            data.put("Stockamount",bigDecimal );
            logger.info(day+"进货金额：" + bigDecimal);
        }
        if (jxcSaleService.getnianamount(day,companyId) == null){
            data.put("Saleamount", "无数据");
            logger.info(day+"销售金额无数据");
        }else {
            BigDecimal bigDecimal = new BigDecimal(jxcSaleService.getnianamount(day, companyId));
            data.put("Saleamount", bigDecimal);
            logger.info(day+"销售金额：" + bigDecimal);
        }
        String amount = "";
        BigDecimal Costamount = new BigDecimal(0);
        if (jxcCostService.getnianamount(day,companyId) != null) {
            amount = jxcCostService.getnianamount(day,companyId);

            Costamount = new BigDecimal(amount);
            logger.info(day+"成本金额：" + Costamount);
            logger.info("------------");
            data.put("Costamount", Costamount);
            if (data.getString("Stockamount").equals("无数据")){
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售无进货有成本
                    data.put("profit","-"+Costamount);
                }else {
                    //有销售无进货有成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(Costamount));
                }
            }else {
                BigDecimal bigDecimal = new BigDecimal(data.getString("Stockamount")).add(Costamount);
                logger.info("有进货有成本");
                logger.info("进货和成本总和"+bigDecimal);
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售有进货有成本
                    data.put("profit","-"+bigDecimal);
                }else {
                    //有销售有进货有成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(bigDecimal));
                    logger.info("有销售有进货有成本");
                }
            }
        }else {
            if (data.getString("Stockamount").equals("无数据")){
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售无进货无成本
                    data.put("profit","无数据");
                }else {
                    //有销售无进货无成本
                    data.put("profit",data.getString("Saleamount"));
                }
            }else {
                if (data.getString("Saleamount").equals("无数据")){
                    //无销售有进货无成本
                    data.put("profit","-"+data.getString("Stockamount"));
                }else {
                    //有销售有进货无成本
                    data.put("profit",new BigDecimal(data.getString("Saleamount")).subtract(new BigDecimal(data.getString("Stockamount"))));
                }
            }

            data.put("Costamount", "无数据");
            logger.info(day+"成本金额无数据");
        }
        result.put("data", data);
        logger.info("result.toString()--------------");
        logger.info(result.toString());
        return result;
    }
    /**
     * 获取登录信息
     *
     * @throws IOException
     * @throws
     */
    @RequestMapping("/getOpenid")
    @ResponseBody
    public JSONObject getOpenid(@RequestBody SystemVo systemVo) throws Exception {

           return jxcUserService.getOpenid(systemVo);

    }
    @GetMapping("/getDefault/{openid}")
    @ApiOperation("查询默认登录人")
    public R getDefault(@PathVariable String openid){
        R result = null;
        try {
            result = new R().ok(jxcUserService.queryDefault(openid));
        } catch (Exception e) {
            result = new R().error(e.getMessage());
        }
        return result;
    }
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param jxcUser 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<JxcUser> page, JxcUser jxcUser) {
        return new R().ok(this.jxcUserService.page(page, new QueryWrapper<>(jxcUser)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return new R().ok(this.jxcUserService.getById(id));
    }

    @PostMapping("/queryall")
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        return new R().ok(jxcUserService.getByConpanyId(companyId));
    }
    /**
     * 新增数据
     *
     * @param json
     * @return 新增结果
     */
    @PostMapping("add")
    public R insert(@RequestBody JSONObject json) {

        return jxcUserService.add(json);
    }

    @PostMapping("getuserinfor")
    public R getuserinfor(@RequestBody JSONObject json) {
        String id = json.getString("id");
        JxcUser jxcUser = jxcUserService.getById(id);
        return new R().ok(jxcUser);
    }
    @PostMapping("getbyphone")
    public R getbyphone(@RequestBody JSONObject json) {
        String phone = json.getString("phone");
        JxcUser jxcUser = jxcUserService.getByphone(phone);
        if (jxcUser == null){
            return new R().ok("未查询到该用户");
        }else {
            return new R().error("手机号已被注册");
        }
    }

    @PostMapping("exdate")
    public R exdate(@RequestBody JSONObject json) {
        return jxcUserService.exdate(json);
    }


    /**
     * 修改数据
     *
     * @param jxcUser 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public R update(@RequestBody JxcUser jxcUser) {
        return new R().ok(this.jxcUserService.updateById(jxcUser));
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        JSONArray idList = params.getJSONArray("idList");
        logger.info("删除数据"+idList);
        return new R().ok(this.jxcUserService.removeByIds(idList));
    }
    /**
     * 短信服务
     *
     * @param
     * @return
     */
    @PostMapping("/DXFW")
    @ApiOperation("短信服务")
    public R DXFW(@RequestBody JSONObject params) {
        R result = null;
        try {
            return jxcUserService.DXFW(params);
        } catch (Exception e) {
            result = new R().error(e.getMessage());
        }
        return result;
    }

    @PostMapping("/YZMYZ")
    @ApiOperation("短信验证服务")
    public R YZMYZ(@RequestBody JSONObject params) {
        R result = null;
        try {
            return jxcUserService.YZMYZ(params);
        } catch (Exception e) {
            result = new R().error(e.getMessage());
        }
        return result;
    }
    @PostMapping("/companyisregister")
    @ApiOperation("公司验证")
    public R companyisregister(@RequestBody JSONObject params) {
        R result = null;
        try {
            return jxcUserService.companyisregister(params);
        } catch (Exception e) {
            result = new R().error(e.getMessage());
        }
        return result;
    }
    @PostMapping("/ivcodeisregister")
    @ApiOperation("邀请码验证")
    public R ivcodeisregister(@RequestBody JSONObject params) {
        R result = null;
        try {
            return jxcUserService.ivcodeisregister(params);
        } catch (Exception e) {
            result = new R().error(e.getMessage());
        }
        return result;
    }

}

